package com.twu.biblioteca;

import java.util.Scanner;

public class LoginUser extends MenuOption {
    public static final String PASSWORD_PROMPT = "Enter your password:";
    public static final String LIBRARY_NO_PROMPT = "Enter your Library Number format(xxx-xxxx):";
    private String libraryNo;
    private String password;
    private  Scanner input = new Scanner(System.in);

    public LoginUser(String name) {
        super(name);
        password = null;
        libraryNo = null;
    }

    @Override
    protected String menuOptionHeader() {
        return "AUTHENTICATION:\n";
    }

    @Override
    protected String menuOptionBody() {
        if (isUserAuthentic()) {
            User user = UserList.getUser(libraryNo);
            App.createUserSession(user);
            return "You have logged in Successfully";
        }
        return "Sorry!! Failed To Authenticate User." +
                "\nPlease ensure that your credentials are correct.\n";

    }

    @Override
    protected String getResponse() {
        return  menuOptionHeader()+ menuOptionBody();
    }

    @Override
    protected boolean hasSubMenu() {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    private boolean isUserAuthentic() {
        libraryNo = promptUserInput(LIBRARY_NO_PROMPT);
        password = promptUserInput(PASSWORD_PROMPT);
        return UserList.authenticateUser(libraryNo, password);
    }

    private String promptUserInput(String flag) {
        BibliotecaApp.print(flag);
        System.out.print(">>");
        String userInput = input.nextLine();
        return userInput;
    }
}
